package com.huirui.server.config;

import com.huirui.server.interceptor.AdminInterceptor;
import com.huirui.server.interceptor.MiniProgramLoginInterceptor;
import com.huirui.server.interceptor.MiniProgramUserInfoInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
@Slf4j
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Resource
    private MiniProgramLoginInterceptor miniProgramLoginInterceptor;

    @Resource
    private MiniProgramUserInfoInterceptor miniProgramUserInfoInterceptor;

    @Resource
    private AdminInterceptor adminInterceptor;

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("注册全局的Token拦截器");
        registry.addInterceptor(miniProgramLoginInterceptor)
                // TODO 细化拦截路径
                .excludePathPatterns("/doc.html") // 排除 Knife4j 的主页面
                .excludePathPatterns("/webjars/**") // 排除 Knife4j 的静态资源
                .excludePathPatterns("/v3/api-docs/**") // 排除 OpenAPI 文档路径
                .excludePathPatterns("/swagger-ui/**") // 如果使用 Swagger UI，也排除其路径
                .excludePathPatterns("/api/category/**") // 排除分类相关API
                .excludePathPatterns("/api/user/code") // 排除获取验证码的API
                .excludePathPatterns("/api/user/login/**") // 排除登录相关API
                .excludePathPatterns("/api/search/**") // 排除搜索商品相关DPI
                .excludePathPatterns("/api/product/**") // 排除商品浏览相关API
                .excludePathPatterns("/api/home/page/**")//排除首页浏览相关API
                .excludePathPatterns("/api/seckill/**")// 排除秒杀商品浏览相关API
                .excludePathPatterns("/api/zone/**") // 排除首页专区浏览相关API
                .excludePathPatterns("/api/ranking/**")// 排除热销商品浏览相关API
                .excludePathPatterns("/api/coupon/list")//排除优惠券浏览相关API
                .excludePathPatterns("/api/newcomer/product/**")// 排除新客专区浏览相关API
                .excludePathPatterns("/api/short/dated/product/**")//排除临期特惠商品相关API
                .excludePathPatterns("/api/brand/**")// 排除品牌浏览相关API
                .excludePathPatterns("/api/user/quickly/login")//排除快捷登录API
                .addPathPatterns("/api/**");
        log.info("注册可能获得用户信息的Token拦截器");
        registry.addInterceptor(miniProgramUserInfoInterceptor)
                .addPathPatterns("/api/**");
        log.info("注册后台管理系统Token拦截器");
        registry.addInterceptor(adminInterceptor)
                .excludePathPatterns("/admin/admin/login")
                .addPathPatterns("/admin/**");
    }


    // 重新映射swagger地址
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * http请求客户端
     */
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有路径
                .allowedOriginPatterns("*") // 允许的来源
                .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH") // 允许的方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true) // 允许携带 cookie
                .maxAge(1800); // 预检请求缓存时间
    }
}
